#ifndef _TBPERLIN_H_
#define _TBPERLIN_H_

#include "TBVec2.h"

#define TB_PERLIN_SAMPLE_SIZE 1024

namespace tb
{

	class Perlin
	{

	public:

		Perlin(unsigned int a_Size);
		Perlin(unsigned int a_Size, int a_Octaves, float a_Frequency, float a_Amplitude, int a_Seed);
		~Perlin();

		void SetOctaves(int a_Octaves);
		void SetFrequency(float a_Frequency);
		void SetAmplitude(float a_Amplitude);

		void Seed(int a_Seed);

		float GetSample(float a_X, float a_Y);
		float GetSample(Vec2 a_Position);

	private:

		void init_perlin(int n,float p);
		float perlin_noise_2D(float a_X, float a_Y);

		float noise1(float arg);
		float noise2(Vec2 a_Position);
		float noise3(float vec[3]);
		void normalize2(float v[2]);
		void normalize3(float v[3]);
		void init(void);

		float m_Size;
		int m_Octaves;
		float m_Frequency;
		float m_Amplitude;
		int m_Seed;

		bool m_Seeded;

		int p[TB_PERLIN_SAMPLE_SIZE + TB_PERLIN_SAMPLE_SIZE + 2];
		float g3[TB_PERLIN_SAMPLE_SIZE + TB_PERLIN_SAMPLE_SIZE + 2][3];
		float g2[TB_PERLIN_SAMPLE_SIZE + TB_PERLIN_SAMPLE_SIZE + 2][2];
		float g1[TB_PERLIN_SAMPLE_SIZE + TB_PERLIN_SAMPLE_SIZE + 2];

	};

}; // namespace tb;

#endif